﻿using System.Globalization;
using InhollandAir.App_Code;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.UI.WebControls;

namespace InhollandAir
{
    public partial class UpdateAccount : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var user = new User();
                DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat; //--MM/dd/yyyy
                if ((Dictionary<string, User>)Application["ActiveUsers"] != null)// No users are logged in
                {
                    Dictionary<string, User> activeUsers = (Dictionary<string, User>)Application["ActiveUsers"];
                    var userLoggedIn = activeUsers.ContainsKey(Session.SessionID) != null ? user.Load(Session.SessionID, activeUsers) : false;
                    if (userLoggedIn)
                    {
                        DateTime dt = DateTime.Parse(user.birthdate, usDtfi);
                        string convertedBirthdate = dt.ToString("MM-dd-yyyy");
                        txt_Firstname.Text = Server.HtmlEncode(user.firstname);
                        txt_Lastname.Text = Server.HtmlEncode(user.lastname);
                        txt_Address.Text = Server.HtmlEncode(user.address);
                        txt_Zipcode.Text = Server.HtmlEncode(user.zipcode);
                        txt_City.Text = Server.HtmlEncode(user.city);
                        txt_Country.Text = Server.HtmlEncode(user.country);
                        txt_Phonenr.Text = Server.HtmlEncode(user.phonenr);
                        txt_DateOfBirth.Text = Server.HtmlEncode(convertedBirthdate);
                        txt_Country.Text = Server.HtmlEncode(user.country);
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
            }
        }

        protected void btn_UpdateAccount_Click(object sender, EventArgs e)
        {

            string firstname = txt_Firstname.Text;
            string lastname = txt_Lastname.Text;
            string address = txt_Address.Text;
            string zipcode = txt_Zipcode.Text;
            string city = txt_City.Text;
            string country = txt_Country.Text;
            string phonenr = txt_Phonenr.Text;
            string birthdate = txt_DateOfBirth.Text;

            Page.Validate();
            if (!Page.IsValid)
            {
                lbl_Error.Text = "Something went wrong, please try again.";
                lbl_Error.Visible = true;
                return;
            }
            try
            {

                DateTime dt = DateTime.Parse(birthdate);
                string convertedBirthdate = dt.ToString("yyyy-MM-dd");
                var user = new User();
                var userExists = user.Load(Session.SessionID, (Dictionary<string, User>)Application["ActiveUsers"]);

                //save in db
                if (userExists)
                {
                    user.firstname = firstname;
                    user.lastname = lastname;
                    user.address = address;
                    user.zipcode = zipcode;
                    user.city = city;
                    user.country = country;
                    user.phonenr = phonenr;
                    user.birthdate = convertedBirthdate;
                    user.Save();
                }
                else
                {
                    lbl_Error.Text = "User not known";
                    return;
                }
                // save/update user in server cache
                Dictionary<string, User> activeUsers = (Dictionary<string, User>)Application["ActiveUsers"];
                List<string> sessionIds = new List<string>();
                foreach(KeyValuePair<string, User> sessionidUser in (Dictionary<string, User>)Application["ActiveUsers"])
                {
                    if (sessionidUser.Value.id == user.id)
                    {
                        //activeUsers[sessionidUser.Key] = user;
                        sessionIds.Add(sessionidUser.Key);
                    }
                }
                foreach (string listItem in sessionIds)
                {
                    activeUsers[listItem] = user;
                }
                Application["ActiveUsers"] = activeUsers;

                Response.Redirect(Request.Cookies["CurrentView"] != null ? "FlightReservations.aspx" : "Account.aspx");
            }
            catch (Exception ex)
            {
                lbl_Error.Text = ex.Message;
            }

        }
    }
}